package com.service.uai.engine.pack;

import java.io.UnsupportedEncodingException;

import com.bpms.common.CommonUtil;
import com.bpms.common.Param;
import com.bpms.common.VO;
import com.bpms.eai.IPackage;
import com.bpms.eai.PackTool;
import com.bpms.esb.SV;

public class ATMServerPack extends PackTool implements IPackage {

	public VO parseBuffer(byte[] buffer) {
//		CommonUtil.debug("ATMServerPack receive:"+new String(buffer));
		try{
//			String len=byteToStr(buffer,0,8);
//			String head=byteToStr(buffer,8,4);
//			int bodylen=Integer.parseInt(len)-24;
//			byte[] body=new byte[bodylen];
//			this.memcpy(body, 0, buffer, 12,bodylen);
			//增加atm的ip验证
			//String atmip=byteToStr(buffer,0,15);
			
//			String len=byteToStr(buffer,15,8);
//			String head=byteToStr(buffer,23,4);
//			int bodylen=Integer.parseInt(len)-24;
//			byte[] body=new byte[bodylen];
//			this.memcpy(body, 0, buffer, 27,bodylen);
			
			String len=byteToStr(buffer,0,8);
			String bseqid=byteToStr(buffer,8,8);
			String bwholelen=byteToStr(buffer,16,8);
			String head=byteToStr(buffer,24,4);
			
			int bodylen=Integer.parseInt(len)-24;
			byte[] body=new byte[bodylen];
			//this.memcpy(body, 0, buffer, 27,bodylen);
			this.memcpy(body, 0, buffer, 32,bodylen);
			CommonUtil.debug("ATMServerPack receive head:"+head);
			CommonUtil.debug("ATMServerPack receive body:"+new String(body));
			VO args=new VO();
			args.setObj("body", body);
			//args.setAttr("atmip", atmip);
			
			String ifVerify=Param.getValue("CONTROL", "IFIPVERIFY");
			VO resultVO=null;
			if("Y".equals(ifVerify))
			{
				resultVO=SV.g("atm.Checkatminfo", args);
			}
			else{
				resultVO=new VO();
			}
			
			args.remove("atmip");
			if(resultVO==null){
				return null;
			}else{
				VO r=null;
				if("2001".equals(head)){					
					r=SV.g("ATM.Parse_atm2001accept", args);
					return r;
				}else if("2002".equals(head)){					
					r=SV.g("ATM.Parse_atm2002accept", args);
					return r;
				}else if("95pj".equals(head)){
					String redirect = Param.getValue("atm", "redirect");//need redirect request 
					if("true".equals(redirect)){
						byte[] request = new byte[Integer.parseInt(len) + 8];
						CommonUtil.debug("----------------------length: " + request.length);
						this.memcpy(request, 0, buffer, 0,request.length);
						CommonUtil.debug("----------------------request: " + new String(request));
						args.setAttr("data",new String(request)); 
						r = SV.g("ATM.Redirect_queryaccount_request", args);
						// 本地保存
						
					}else{ //use local account info
						CommonUtil.debug("use local account info with:args" + args);
						r = SV.g("ATM.Parse_atm_account", args);
					}
					
				}else if("1008".equals(head)){			
					r = SV.g("ATM.Parse_atm_bill", args);
				}else if("1001".equals(head)){
					// 接收支票影像
					CommonUtil.debug("recieve suzhou atm-a checkquee image.......................:args" + args);
					byte[] request = new byte[Integer.parseInt(len) + 8];
					this.memcpy(request, 0, buffer, 0,request.length);
//					args.setAttr("data", new String(request));
					r = SV.g("ATM.Parse_atm_check_image", args);
				}else if("1002".equals(head)){
					// 接收进帐单
					CommonUtil.debug("recieve suzhou atm-a into check.......................:args" + args);
					byte[] request = new byte[Integer.parseInt(len) + 8];
					this.memcpy(request, 0, buffer, 0,request.length);
//					args.setAttr("data", new String(request));
					r = SV.g("ATM.Parse_atm_into_account", args);
				} else if ("1000".equals(head)) {
					// 接收账号校验
					CommonUtil.debug("recieve suzhou atm-a account check.......................:args" + args);
					r = SV.g("ATM.Prase_atm_account_check", args);
				}
				return r;
			}
		}
		catch(Exception e)
		{
			CommonUtil.debug("ATMServerPack parseBuffer exception",e);
		}
		return null;
	}

	public byte[] prepareBuffer(String svname, VO args) {
		CommonUtil.debug("prepareBuffer svname is " + svname +" args:" + args);
		
		if("ATM.Sendhdquerymsg".equals(svname)){
			//组回执报文
			VO v=SV.g("ATM.Build_hdquerylist", args);
			String msg=v.getAttr("msg");
			byte[] buf=null;
			try {
				buf=msg.getBytes("gb2312");
				//byte len[]=PackageTool.intToByte2(buf.length);
				//return memmerge(len, buf);
				return buf;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				CommonUtil.debug("ATMServerPack.prepareBuffer exception",e);
			}
		}
		else if ("ATM.Sendhdprintmsg".equals(svname))
		{
//			组回执报文
			VO v=SV.g("ATM.Build_hdprintlist", args);
			String msg=v.getAttr("msg");
			byte[] buf=null;
			try {
				buf=msg.getBytes("gb2312");
				//byte len[]=PackageTool.intToByte2(buf.length);
				//return memmerge(len, buf);
				return buf;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				CommonUtil.debug("ATMServerPack.prepareBuffer exception",e);
			}
		}else if ("ATM.Savebillimage".equals(svname))
		{
			CommonUtil.debug("ATM.Sendbillsavemsg. done.");//			组回执报文
			
			String msg = args.getAttr("msg");
			byte[] buf=null;
			try {
				buf = msg.trim().getBytes("gb2312");
				return buf;
			} catch (UnsupportedEncodingException e) {
				CommonUtil.debug("ATMServerPack.prepareBuffer exception",e);
			}
		}
		else if ("ATM.Redirect_queryaccount_request_answer".equals(svname))
		{		
			String msg=args.getAttr("msg");
			byte[] buf=null;
			try {
				buf=msg.trim().getBytes("gb2312");
				CommonUtil.debug("ATMServerPack.prepareBuffer buf_len:" + buf.length + " buf:" + msg.trim());
				return buf;//send
			} catch (UnsupportedEncodingException e) {
				CommonUtil.debug("ATMServerPack.prepareBuffer exception",e);
			}
		}else if ("ATM.Answer_atm_account".equals(svname))
		{		
			String msg=args.getAttr("msg");
			byte[] buf=null;
			try {
				buf=msg.trim().getBytes("gb2312");
				CommonUtil.debug("ATMServerPack.prepareBuffer buf_len:" + buf.length + " buf:" + msg.trim());
				return buf;//send
			} catch (UnsupportedEncodingException e) {
				CommonUtil.debug("ATMServerPack.prepareBuffer exception",e);
			}
		}
		else if ("ATM.Answer_atm_check_image".equals(svname))
		{	// 回执报文 支票图像
			String msg=args.getAttr("msg");
			byte[] buf=null;
			try {
				buf=msg.trim().getBytes("gb2312");
				CommonUtil.debug("ATMServerPack.prepareBuffer buf_len:" + buf.length + " buf:" + msg.trim());
				return buf;//send
			} catch (UnsupportedEncodingException e) {
				CommonUtil.debug("ATMServerPack.prepareBuffer exception",e);
			}
		}
		else if ("ATM.Answer_atm_into_account".equals(svname))
		{	// 回执报文 进帐单
			String msg=args.getAttr("msg");
			byte[] buf=null;
			try {
				buf=msg.trim().getBytes("gb2312");
				CommonUtil.debug("ATMServerPack.prepareBuffer buf_len:" + buf.length + " buf:" + msg.trim());
				return buf;//send
			} catch (UnsupportedEncodingException e) {
				CommonUtil.debug("ATMServerPack.prepareBuffer exception",e);
			}
		}
		else if ("ATM.Answer_atm_account_check".equals(svname)) {
			// 账号校验
			String msg=args.getAttr("msg");
			byte[] buf=null;
			try {
				buf=msg.trim().getBytes("gb2312");
				CommonUtil.debug("ATMServerPack.prepareBuffer buf_len:" + buf.length + " buf:" + msg.trim());
				return buf;//send
			} catch (UnsupportedEncodingException e) {
				CommonUtil.debug("ATMServerPack.prepareBuffer exception",e);
			}
		}
		return null;
	}

}
